<?php
namespace BP\DA;
//require_once("java/util/ArrayList.php");
//require_once("java/util/Hashtable.php");
require_once("BP/Difference/ContextHolderUtils.php");
require_once("BP/En/Attr.php");
require_once("BP/En/Attrs.php");
require_once("BP/En/ClassFactory.php");
require_once("BP/En/Entities.php");
require_once("BP/En/Entity.php");
require_once("BP/En/Map.php");
require_once("BP/En/SQLCash.php");
require_once("BP/En/SqlBuilder.php");
require_once("BP/Sys/SysEnum.php");
require_once("BP/Sys/SysEnumAttr.php");
require_once("BP/Sys/SysEnums.php");
require_once("BP/Sys/SystemConfig.php");
require_once("BP/Tools/ConvertTools.php");
require_once("BP/Tools/StringHelper.php");
use BP\En\Entities;
use BP\En\Map;
use BP\En\SQLCash;
use Exception;
class Cash {
	public static $EnumKeySQL_Cash;	// Hashtable<String, String>
	public static $CS_Cash;	// Hashtable<String, Object>
	protected static $_Bill_Cash;	// Hashtable
	protected static $_BS_Cash;	// Hashtable<String, Object>
	protected static $_SQL_Cash;	// Hashtable<String, Object>
	protected static $_EnsData_Cash;	// Hashtable<String, Object>
	protected static $_EnsData_Cash_Ext;	// Hashtable<String, Object>
	protected static $_Map_Cash;	// Hashtable<String, Object>
	public static function getCaseWhenSQL ($mTable, $key, $field, $enumKey, $def, $tag) // [String mTable, String key, String field, String enumKey, int def, String tag]
	{
		$mykey = ((((((($mTable . "") . $key) . "") . $field) . "") . $enumKey) . $def);
		if ((self::$EnumKeySQL_Cash == NULL))
			self::$EnumKeySQL_Cash = []; //map
		$sql = self::$EnumKeySQL_Cash->get($mykey);
		if (($sql . NULL))
			return $sql;
		$ses = NULL;
		if ((DataType::IsNullOrEmpty($tag) ==  TRUE ))
		{
			$ses = new SysEnums($enumKey);
			if (($ses->size() == 0))
			{
				$xml = new EnumInfoXml($enumKey);
				$ses->RegIt($enumKey, $xml->getVals());
			}
		}
		else
		{
			$ses = new SysEnums();
			$ses->RegIt($enumKey, $tag);
		}
		$sql = ((" CASE " . $mTable) . $field);
		foreach ($ses->ToJavaList() as $se1) 		{
			$sql += ((((" WHEN " . $se1->getIntKey()) . " THEN '") . $se1->getLab()) . "'");
		}
		$se = $ses->GetEntityByKey($SysEnumAttr->IntKey, $def);
		if (($se == NULL))
		{
			$sql += ((" END \"" . $key) . "Text\"");
		}
		else
		{
			$sql += ((((" WHEN NULL THEN '" . $se->getLab()) . "' END \"") . $key) . "Text\"");
		}
		self::$EnumKeySQL_Cash->put($mykey, $sql);
		return $sql;
	}
	public static function getBill_Cash () 
	{
		if ((self::$_Bill_Cash == NULL))
		{
			self::$_Bill_Cash = []; //map
		}
		return self::$_Bill_Cash;
	}
	public static function getBS_Cash () 
	{
		if ((self::$_BS_Cash == NULL))
		{
			self::$_BS_Cash = []; //map
		}
		return self::$_BS_Cash;
	}
	public static function ClearCash () 
	{
		if ((self::$_BS_Cash != NULL))
			self::$_BS_Cash->clear();
		if ((self::$_SQL_Cash != NULL))
			self::$_SQL_Cash->clear();
		if ((self::$_EnsData_Cash != NULL))
			self::$_EnsData_Cash->clear();
		if ((self::$_Map_Cash != NULL))
			self::$_Map_Cash->clear();
		if ((self::$_EnsData_Cash_Ext != NULL))
			self::$_EnsData_Cash_Ext->clear();
		if ((self::$_Bill_Cash != NULL))
			self::$_Bill_Cash->clear();
	}
	public static function getSQL_Cash () 
	{
		if ((self::$_SQL_Cash == NULL))
		{
			self::$_SQL_Cash = []; //map
		}
		return self::$_SQL_Cash;
	}
	public static function GetSQL ($clName) // [String clName]
	{
		$tempVar = Cash::getSQL_Cash()->get($clName);
		return (( (($tempVar instanceof SQLCash)) ? $tempVar : NULL ));
	}
	public static function SetSQL ($clName, $csh) // [String clName, SQLCash csh]
	{
		if ((($clName . NULL) . ($csh == NULL)))
		{
			throw new Exception("clName.  csh 参数有一个为空�??");
		}
		Cash::getSQL_Cash()->put($clName, $csh);
	}
	public static function InitSQL () 
	{
		$al = $ClassFactory->GetObjects("Entity");
		foreach ($al as $en) 		{
			$sql = $SqlBuilder->Retrieve($en);
		}
	}
	public static function getEnsData_Cash () 
	{
		if ((self::$_EnsData_Cash == NULL))
		{
			self::$_EnsData_Cash = []; //map
		}
		return self::$_EnsData_Cash;
	}
	public static function GetEnsData ($clName) // [String clName]
	{
		$tempVar = Cash::getEnsData_Cash()->get($clName);
		$ens = (( (($tempVar instanceof Entities)) ? $tempVar : NULL ));
		if (($ens == NULL))
		{
			return NULL;
		}
		if (($ens->size() == 0))
		{
			return NULL;
		}
		return $ens;
	}
	public static function EnsDataSet ($clName, $obj) // [String clName, Entities obj]
	{
		if (($obj->size() == 0))
		Cash::getEnsData_Cash()->put($clName, $obj);
	}
	public static function remove ($clName) // [String clName]
	{
		Cash::getEnsData_Cash()->remove($clName);
	}
	public static function getEnsData_Cash_Ext () 
	{
		if ((self::$_EnsData_Cash_Ext == NULL))
		{
			self::$_EnsData_Cash_Ext = []; //map
		}
		return self::$_EnsData_Cash_Ext;
	}
	public static function GetEnsDataExt ($clName) // [String clName]
	{
		if ($SystemConfig->getIsTempCashFail())
		{
			Cash::getEnsData_Cash_Ext()->clear();
			return NULL;
		}
		try 
		{
			$ens = null;
			$tempVar = Cash::getEnsData_Cash_Ext()->get($clName);
			$ens = (( (($tempVar instanceof Entities)) ? $tempVar : NULL ));
			return $ens;
		}
		catch (\Exception $e)
		{
			return NULL;
		}
	}
	public static function SetEnsDataExt ($clName, $obj) // [String clName, Entities obj]
	{
		if ((($clName . NULL) . ($obj == NULL)))
		{
			throw new \ErrorException("clName.  obj 参数有一个为空�??");
		}
		Cash::getEnsData_Cash_Ext()->put($clName, $obj);
	}
	public static function getMap_Cash () 
	{
		if ((self::$_Map_Cash == NULL))
		{
			self::$_Map_Cash = []; //map
		}
		return self::$_Map_Cash;
	}
	public static function GetMap ($clName) // [String clName]
	{
		try 
		{
			$tempVar = Cash::getMap_Cash()->get($clName);
			return (( (($tempVar instanceof Map)) ? $tempVar : NULL ));
		}
		catch (\Exception $e)
		{
			return NULL;
		}
	}
	public static function SetMap ($clName, $map) // [String clName, Map map]
	{
		if (($clName . NULL))
			return ;
		if (($map == NULL))
		{
			Cash::getMap_Cash()->remove($clName);
			return ;
		}
		Cash::getMap_Cash()->put($clName, $map);
	}
	public static function GetObj_String_Depositary ($key, $where) // [String key, Depositary where]
	{
		if (($where == Depositary::$None))
		{
			throw new \Exception((("您没有把(" . $key) . ")放到session or application 里面不能找出他们."));
		}
		if ($SystemConfig->getIsBSsystem())
		{
			if (($where == Depositary::$Application))
			{
				return Cash::getBS_Cash()->get($key);
			}
			else
			{
				return $ContextHolderUtils->getSession()->getAttribute($key);
			}
		}
		else
		{
			return self::$CS_Cash->get($key);
		}
	}
	public static function GetObj_String ($key) // [String key]
	{
		if ($SystemConfig->getIsBSsystem())
		{
			$obj = Cash::getBS_Cash()->get($key);
			if (($obj == NULL))
			{
				$obj = Cash::GetObjFormSession($key);
			}
			return $obj;
		}
		else
		{
			return self::$CS_Cash->get($key);
		}
	}
	public static function DelObjFormApplication ($likeKey) // [String likeKey]
	{
		$i = 0;
		if ($SystemConfig->getIsBSsystem())
		{
			$willDelKeys = "";
			foreach (Cash::getBS_Cash()->keySet() as $key) 			{
				if (!$key->contains($likeKey))
				{
					continue;
				}
				$willDelKeys += ("@" . $key);
			}
// 			$strs = $willDelKeys->split("(@)", -1);
			$strs = explode('@', $willDelKeys, -1);
			foreach ($strs as $s) 			{
				if ((($s == NULL) || $s->equals("")))
				{
					continue;
				}
				Cash::getBS_Cash()->remove($s);
				++$i;
			}
		}
		else
		{
			$willDelKeys = "";
			foreach (self::$CS_Cash->keySet() as $key) 			{
				if (!$key->contains($likeKey))
				{
					continue;
				}
				$willDelKeys += ("@" . $key);
			}
			$strs = explode('@', $willDelKeys, -1);
			foreach ($strs as $s) 			{
				if ((($s == NULL) || $s->equals("")))
				{
					continue;
				}
				self::$CS_Cash->remove($s);
				++$i;
			}
		}
		return $i;
	}
	public static function GetObjFormApplication ($key, $isNullAsVal) // [String key, Object isNullAsVal]
	{
		if ($SystemConfig->getIsBSsystem())
		{
			$obj = Cash::getBS_Cash()->get($key);
			if (($obj == NULL))
			{
				return $isNullAsVal;
			}
			else
			{
				return $obj;
			}
		}
		else
		{
			$obj = self::$CS_Cash->get($key);
			if (($obj == NULL))
			{
				return $isNullAsVal;
			}
			else
			{
				return $obj;
			}
		}
	}
	public static function GetObjFormSession ($key) // [String key]
	{
		if ($SystemConfig->getIsBSsystem())
		{
			try 
			{
				return $ContextHolderUtils->getSession()->getAttribute($key);
			}
			catch (\Exception $e)
			{
				return NULL;
			}
		}
		else
		{
			return self::$CS_Cash->get($key);
		}
	}
	public static function RemoveObj_String_Depositary ($key, $where) // [String key, Depositary where]
	{
		if (!Cash::IsExits($key, $where))
		{
			return ;
		}
		if ($SystemConfig->getIsBSsystem())
		{
		    if (($where == Depositary::$Application)){
		        
		    }
			else
			{
				$ContextHolderUtils->getSession()->removeAttribute($key);
			}
		}
		else
		{
			self::$CS_Cash->remove($key);
		}
	}
	public static function RemoveObj_String ($key) // [String key]
	{
		Cash::getBS_Cash()->remove($key);
	}
	public static function AddObj ($key, $where, $obj) // [String key, Depositary where, Object obj]
	{
		if (($key . NULL))
		{
			throw new \Exception((("您需要为obj=" . $obj->toString()) . ",设置为主键�?��?�key"));
		}
		if (($obj == NULL))
		{
			throw new \Exception(("您需要为obj=null  设置为主键�?��?�key=" . $key));
		}
		if (($where == Depositary::$None))
		{
			throw new \Exception((("您没有把(" . $key) . ")放到 session or application 里面设置他们."));
		}
		if ($SystemConfig->getIsBSsystem())
		{
			if (($where == Depositary::$Application))
			{
				Cash::getBS_Cash()->put($key, $obj);
			}
			else
			{
				$ContextHolderUtils->getSession()->setAttribute($key, $obj);
			}
		}
		else
		{
			if (self::$CS_Cash->containsKey($key))
			{
				self::$CS_Cash->put($key, $obj);
			}
			else
			{
				self::$CS_Cash->put($key, $obj);
			}
		}
	}
	public static function IsExits ($key, $where) // [String key, Depositary where]
	{
		if ($SystemConfig->getIsBSsystem())
		{
			if (($where == Depositary::$Application))
			{
				return  TRUE ;
			}
			else
			{
				return  TRUE ;
			}
		}
		else
		{
			return self::$CS_Cash->containsKey($key);
		}
	}
	public static function GetBillStr ($cfile, $isCheckCash) // [String cfile, boolean isCheckCash]
	{
		$val = (( ((Cash::getBill_Cash()->get($cfile) instanceof String)) ? Cash::getBill_Cash()->get($cfile) : NULL ));
		if (($isCheckCash ==  TRUE ))
		{
			$val = NULL;
		}
		if (is_null($val) || $val == '')
		{
			$file = NULL;
			if ($cfile->contains(":"))
			{
				$file = $cfile;
			}
			else
			{
				$file = (($SystemConfig->getPathOfDataUser() . "CyclostyleFile/") . $cfile);
			}
			try 
			{
				$val = $ConvertTools->StreamReaderToStringConvert($file, "us-ascii");
			}
			catch (Exception $ex)
			{
				throw new \Exception(((("@读取单据模板时出现错误�?�cfile=" . $cfile) . " @Ex=") . $ex->getMessage()));
			}
			self::$_Bill_Cash->put($cfile, $val);
		}
		return $val->substring(0);
	}
	public static function GetBillParas_String_String_Entities ($cfile, $ensStrs, $ens) // [String cfile, String ensStrs, Entities ens]
	{
		$paras = (( ( is_array(Cash::getBill_Cash()->get(($cfile . "Para")) )) ? Cash::getBill_Cash()->get(($cfile . "Para")) : NULL ));
		if (($paras != NULL))
		{
			return $paras;
		}
		$attrs = new Attrs();
		foreach ($Entities->convertEntities($ens) as $en) 		{
			$perKey = $en;
			$enAttrs = $en->getEnMap()->getAttrs();
			foreach ($Attrs->convertAttrs($enAttrs) as $attr) 			{
				$attrN = new Attr();
				$attrN->setKey((($perKey . ".") . $attr->getKey()));
				if ($attr->getIsRefAttr())
				{
					$attrN->setField(((($perKey . ".") . $attr->getKey()) . "Text"));
				}
				$attrN->setMyDataType($attr->getMyDataType());
				$attrN->setMyFieldType($attr->getMyFieldType());
				$attrN->setUIBindKey($attr->getUIBindKey());
				$attrN->setField($attr->getField());
				$attrs->Add($attrN);
			}
		}
		$paras = Cash::GetBillParas_Gener($cfile, $attrs);
		self::$_Bill_Cash->put(($cfile . "Para"), $paras);
		return $paras;
	}
	public static function GetBillParas_String_String_Entity ($cfile, $ensStrs, $en) // [String cfile, String ensStrs, Entity en]
	{
		$paras = (( (is_array(Cash::getBill_Cash()->get(($cfile . "Para")))) ? Cash::getBill_Cash()->get(($cfile . "Para")) : NULL ));
		if (($paras != NULL))
		{
			return $paras;
		}
		$paras = Cash::GetBillParas_Gener($cfile, $en->getEnMap()->getAttrs());
		self::$_Bill_Cash->put(($cfile . "Para"), $paras);
		return $paras;
	}
	public static function GetBillParas_Gener ($cfile, $attrs) // [String cfile, Attrs attrs]
	{
		$paras = array();
		$Billstr = Cash::GetBillStr($cfile,  TRUE );
		$chars = $Billstr->toCharArray();
		$para = "";
		$i = 0;
		$haveError =  FALSE ;
		$msg = "";
		foreach ($chars as $c) 		{
			if (($c . '>'))
			{
				$real = $para->toString();
				if ((($attrs != NULL) && $real->contains(" ")))
				{
					$real = $real->replace(" ", "");
					$Billstr = $Billstr->replace($para, $real);
					$para = $real;
					$haveError =  TRUE ;
				}
				if (((($attrs != NULL) && $real->contains("\\")) && ($real->contains("ND") ==  FALSE )))
				{
					$haveError =  TRUE ;
					$findKey = NULL;
					$keyLen = 0;
					foreach ($attrs as $attr) 					{
						if ($real->contains($attr->getKey()))
						{
							if (($keyLen <= $attr->getKey()->length()))
							{
								$keyLen = $attr->getKey()->length();
								$findKey = $attr->getKey();
							}
						}
					}
					if (($findKey . NULL))
					{
						$msg += (("@参数:<font color=red><b>(" . $real) . ")</b></font>可能拼写错误�?");
						continue;
					}
					if (($real->contains(($findKey . ".NYR")) ==  TRUE ))
					{
						$real = ($findKey . ".NYR");
					}
					else
						if (($real->contains(($findKey . ".RMB")) ==  TRUE ))
						{
							$real = ($findKey . ".RMB");
						}
						else
							if (($real->contains(($findKey . ".RMBDX")) ==  TRUE ))
							{
								$real = ($findKey . ".RMBDX");
							}
							else
							{
								$real = $findKey;
							}
					$Billstr = $Billstr->replace($para, $real);
				}
				$paras[$i] = $para;
				++$i;
			}
			if (($c . '<'))
			{
				$para = "";
			}
			else
			{
				if ((new Character($c))->toString()->equals(""))
				{
					continue;
				}
				$para += (new Character($c))->toString();
			}
		}
		if ($haveError)
		{
			$myfile = (($SystemConfig->getPathOfDataUser() . "/CyclostyleFile/") . $cfile);
			if (!file_exists($myfile))
			{
				$myfile = $cfile;
			}
			try 
			{
				$ConvertTools->StreamWriteConvert($Billstr, $myfile);
			}
			catch (\Exception $e)
			{
				$e->getMessage(); // 
			}
		}
		if (!$msg->equals(""))
		return $paras;
	}
}
?>
